﻿#region License
/*
Copyright (c) 2005-2008, CellAO Team

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the CellAO Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Using
using System;
using System.Collections.Generic;
using System.Text;
using AO.Core;
using Cell.Core;
#endregion

namespace ZoneEngine
{
    public class Character : CharacterBase
    {
        public Client client;
        public bool needpurge = true;
        public const float PredictionLength = 0.5f;

        #region Constructor / Destructor
        public Character(int characterId, Client client)
            : base(characterId)
        {
            this.client = client;

            stats = new CharacterStats();

            if (characterId != 0)
            {
                ReadCoordinates();
                ReadStats();
            }
        }

        ~Character()
        {
            Purge();
        }
        #endregion

        public void Purge()
        {
            if ((characterId != 0) && (needpurge))
            {
                needpurge = false;

                WriteCoordinates();
                WriteStats();
            }
        }

        #region Coordinates
        public float coordX;
        public float coordY;
        public float coordZ;
        public float coordH1;
        public float coordH2;
        public float coordH3;
        public float coordH4;
        public int pf;

        //these will contain the server's position prediction based on the current and the last coordinates
        //calculation requires: Current position, Current heading, Current estimated speed
        //private Vector3 _predictCoords;

        //public Vector3 PredictCoords
        //{
        //    get { PredictPosition(); return _predictCoords; }
        //}

        /// <summary>
        /// Predict the character's future position.
        /// Take the current and previous positions and calculate the char's speed.
        /// </summary>
        //private void PredictPosition()
        //{
        //    if (_previousCoords == new Vector3())
        //    {
        //        _previousCoords.X = _coords.X;
        //        _previousCoords.Y = _coords.Y;
        //        _previousCoords.Z = _coords.Z;
        //    }

        //    float distance = Vector3.Distance(_coords, _previousCoords);
        //    Vector3 direction = Vector3.Normalize(_coords - _previousCoords);
        //    Vector3 predict_unbound = Vector3.Multiply(direction, distance * PredictionLength); //this vector isn't bound to a specific location, but has a defined direction and length
        //    _predictCoords = Vector3.Add(predict_unbound, _coords); //now we bind the unbound vector to a start position (ie. current coordinates) and get the predicted location
        //}

        public bool ReadCoordinates()
        {
            mySql.MySqlRead("SELECT `ZoneID`, `X`, `Y`, `Z` FROM `characters` WHERE ID = '" + characterId + "' LIMIT 1");

            if (!mySql.myreader.Read())
            {
                mySql.myreader.Close();

                mySql.mcc.Close();

                return false;
            }

            pf = mySql.myreader.GetInt32(0);
            coordX = (float)mySql.myreader.GetInt32(1);
            coordY = (float)mySql.myreader.GetInt32(2);
            coordZ = (float)mySql.myreader.GetInt32(3);
            coordH1 = 0.0f;
            coordH2 = 0.0f;
            coordH3 = 0.0f;
            coordH4 = 0.0f;

            mySql.myreader.Close();

            mySql.mcc.Close();

            return true;
        }

        public bool WriteCoordinates()
        {
            mySql.MySqlUpdate(String.Format("update `characters` SET `ZoneID` = '{0}', `X` = '{1}', `Y` = '{2}', `Z` = '{3}' WHERE `ID` = '{4}'",
                pf, (int)coordX, (int)coordY, (int)coordZ, characterId));

            return true;
        }
        #endregion

        #region Stats
        public CharacterStats stats;

        public bool WriteStats()
        {
            return stats.WriteStats(this);
        }

        public bool ReadStats()
        {
            return stats.ReadStats(this);
        }
        #endregion

        public byte[] spawn { get; set; }
    }
}
